//
//  ModifyPasswordViewController.m
//  tuanGG
//
//  Created by 微他 on 15/9/25.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "Header.h"
#import "ModifyPasswordSaveController.h"
@interface ModifyPasswordViewController ()
@property (nonatomic,strong)UITextField *phoneTxt;
@property (nonatomic,strong)UIView *phoneleftView;
@property (nonatomic,strong)UITextField *authCode;
@property (nonatomic,strong)UIView *pswleftView;
@property (nonatomic,strong)UIButton *nextButton;
@property (nonatomic,strong)UIButton *authCodeBtn;
@end

@implementation ModifyPasswordViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBACOLOR(236, 235, 232, 1);
    [self initNav];
    [self initPassword];
}
- (void)initNav {
    self.navigationController.navigationBar.frame = CGRectMake(0, -64, ScreenWidth, 64);
    UIView *nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, self.navigationController.navigationBar.frame.size.height)];
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, nav.frame.size.height-35, 30, 30)];
    [backButton setImage:IMGNAME(@"back") forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    nav.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, nav.frame.size.height-30, ScreenWidth, 20)];
    label.text = @"验证手机号";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:font_Font size:20];
    label.textAlignment = NSTextAlignmentCenter;
    [nav addSubview:label];
    [nav addSubview:backButton];
    [self.view addSubview:nav];
}
- (void)initPassword {
    self.view.userInteractionEnabled = YES;
    UIView * loginView = [[UIView alloc]initWithFrame:CGRectMake(15,64+15, ScreenWidth - 30, 90)];
    [loginView.layer setCornerRadius:5.0];
    loginView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:loginView];
    
    //账号
    _phoneTxt = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - 30, 45)];
    _phoneTxt.placeholder = @"请输入手机号码";
//    _phoneTxt.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTxt.font = BOLD_FONT(12);
    _phoneTxt.text = nil;
    _phoneTxt.keyboardType = UIKeyboardTypeNumberPad;
    [loginView addSubview:_phoneTxt];
    
    
    _phoneleftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 45)];
    [self.view addSubview:_phoneleftView];
    UIImageView * phoneleft = [[UIImageView alloc]initWithFrame:CGRectMake(10, 13.5*PRO, 17, 18)];
    phoneleft.image = [UIImage imageNamed:@"AcountLeftimage.png"];
    [_phoneleftView addSubview:phoneleft];
    _phoneTxt.leftViewMode = UITextFieldViewModeAlways;
    _phoneTxt.leftView = _phoneleftView;
    
    //验证码
    _authCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _authCodeBtn.frame = CGRectMake(CGRectGetMaxX(self.phoneTxt.bounds)-70, 7.5, 60, 30);
    _authCodeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _authCodeBtn.titleLabel.numberOfLines = 2;
//    [_authCodeBtn setBackgroundImage:[UIImage imageNamed:@"authCodeBtnImage.png"] forState:UIControlStateNormal];
    [_authCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_authCodeBtn setTitleColor:[UIColor colorWithHexString:@"33ccbf"] forState:UIControlStateNormal];
    [_authCodeBtn addTarget:self action:@selector(processbtnAuthClick) forControlEvents:UIControlEventTouchUpInside];
    _authCodeBtn.tag = 110;
    _authCodeBtn.layer.cornerRadius = 5;
    _authCodeBtn.layer.borderWidth = 1;
    _authCodeBtn.layer.borderColor = [[UIColor colorWithHexString:@"33ccbf"]CGColor];
    _authCodeBtn.titleLabel.font = BOLD_FONT(11);
    [self.phoneTxt addSubview:_authCodeBtn];

    
    
    //密码
    _authCode = [[UITextField alloc]initWithFrame:CGRectMake(0, 45, ScreenWidth - 30, 45)];
    _authCode.placeholder = @"请输入验证码";
    _authCode.clearButtonMode = UITextFieldViewModeWhileEditing;
    _authCode.font = BOLD_FONT(12);
//    _passwordTxt.secureTextEntry = YES;
    _authCode.text  = nil;
    [loginView addSubview:_authCode];
    
    
    _pswleftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 45)];
    UIImageView *pswleft = [[UIImageView alloc]initWithFrame:CGRectMake(10, 12*PRO, 15, 19)];
    pswleft.image = [UIImage imageNamed:@"authCodeLeftimage.png"];
    [_pswleftView addSubview:pswleft];
    _authCode.leftViewMode = UITextFieldViewModeAlways;
    _authCode.leftView = _pswleftView;
    
    
    //分割线
    UIView * lineView = [[UIView alloc ]initWithFrame:CGRectMake(0, 45, ScreenWidth-30, 0.6)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"c8c8c8" alpha:0.5];
    [loginView addSubview:lineView];
    
    //下一步按钮
    _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextButton.frame = CGRectMake(15, CGRectGetMaxY(loginView.bounds) + 148, ScreenWidth - 30, 40);
    _nextButton.backgroundColor = [UIColor colorWithHexString:@"33ccbf"];
    _nextButton.titleLabel.font = BOLD_FONT(15);
    [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [_nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_nextButton.layer setCornerRadius:5.0];
    [_nextButton addTarget:self action:@selector(processBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _nextButton.tag = 200;
    [self.view addSubview:_nextButton];
}


#pragma mark - 动作实现
- (void)backButton {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)processBtnClick{
    if ([_phoneTxt.text isEqualToString:@""]) {

        [CustomAlertView alterSureMsg:@"请输入手机号码" delegate:nil];
    }
    else
    {
        if ([_authCode.text isEqualToString:@""]) {
            [CustomAlertView alterSureMsg:@"验证码不能为空" delegate:nil];
        }
        else
        {
                if (_authCode.text.length != 4) {
                    [CustomAlertView alterSureMsg:@"请输入4位数的验证码" delegate:nil];
                }
                else
                {
                    
                    [self Networking];

                }
        }
    }
}
- (void)processbtnAuthClick{
    NSLog(@"点击");
    if ([_phoneTxt.text isEqualToString:@""]) {
        [Common addAlert:@"请输入您的手机号码"];

    }
    else
    {
        if (![ValidateMobileWithEmail validateMobile:_phoneTxt.text]) {
            [Common addAlert:@"手机号码格式不正确"];
        }
        else
        {
            [CustomAlertView alterMsg:[NSString stringWithFormat:@"是否是通过%@号码获取验证码吗?",self.phoneTxt.text] delegate:self];
        }

    }
}
#pragma amrk - <UIAlertViewDelegate>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"sssssss");
        //获取验证码
        [Common verificationCode:^{
            [_authCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            _authCodeBtn.userInteractionEnabled = YES;
        } blockNo:^(id time) {
            [_authCodeBtn setTitle:[NSString stringWithFormat:@"获取验证码\n     %@'s",time] forState:UIControlStateNormal];
            NSLog(@"%@",_authCodeBtn.titleLabel.text);
            _authCodeBtn.userInteractionEnabled = NO;
            //发送验证
        }];

        //发送验证信息
//        [SMSSDK getVerificationCodeBySMSWithPhone:self.phoneTxt.text zone:@"86"   result:^(SMS_SDKError *error) {
//
//            if (!error) {
//
//                NSLog(@"");
//            }
//            else
//            {
//                NSLog(@"错误:%@",error.description);
//                [CustomAlertView alterSureMsg:[NSString stringWithFormat:@"%@",error.description] delegate:nil];
//            }
//
//        }];

       [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_phoneTxt.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
           if (!error) {
               [Common addAlert:@"发送成功"];
           }else{
               [Common addAlert:@"发送失败"];
           }

       }];
    }
}

#pragma mark - 网络请求
- (void)Networking {

    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params setObject:_phoneTxt.text forKey:@"tel"];//手机号码
    [params setObject:_authCode.text forKey:@"vcode"];//验证码

    [GLAFRequest startRequest:alterCenterPswCode paramsDic:params defaultdialog:YES ispost:NO handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"修改密码:%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            ModifyPasswordSaveController *modify = [[ModifyPasswordSaveController alloc]init];
            modify.phoneNumber = _phoneTxt.text;
            [self.navigationController pushViewController:modify animated:YES];
        }
    } doFinally:^(NSDictionary *paramsDic) {
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
@end
